package com.itita.weibo.model;

import java.util.HashMap;
import java.util.List;

import weibo4andriod.User;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.itita.weibo.manager.MainService;
import com.itita.weibo.manager.WeiboManager;
import com.itita.weibo.ui.AccountManagerActivity;
import com.itita.weibo.ui.R;
import com.itita.weibo.util.WeiboUtil;

/**
 * 更多
 * @author 焦明 2010-5-10 3:40
 *
 */
public final class AccountManagerAdapter extends BaseAdapter{
    
	private Context context;
	
	public  List<User> list;
	
	private LayoutInflater inflater;
	
	public AccountManagerAdapter(Context context, List<User> list){
		this.context = context;
		this.list = list;
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this.list.size()+1;
	}

	@Override
	public Object getItem(int position) {
		if(position == this.getCount()-1){ //更多
			return null;
		}
		return this.list.get(position);
	}

	@Override
	public long getItemId(int position) {
		if(position < this.getCount()-1){
			return position;
		}
		return -1;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		////System.out.println(position);
		if(convertView == null){
			convertView = LayoutInflater.from(context).inflate(R.layout.accountlistitemview, null);
		}
		ImageView ivPoint = (ImageView) convertView.findViewById(R.id.ivPoint);
		ImageView ivTrianle = (ImageView) convertView.findViewById(R.id.ivTrianle);
		
		if(position == this.getCount()-1){ //更多
			TextView tv = (TextView) convertView.findViewById(R.id.tvAccountName); 
			tv.setText("添加新账号");
			ivPoint.setImageResource(R.drawable.account_item_add);
			ivTrianle.setVisibility(View.GONE);
			return convertView;	
		}else{
			TextView tvAccountName = (TextView) convertView.findViewById(R.id.tvAccountName); 
			tvAccountName.setText(this.list.get(position).getUsername()); 
			if(MainService.currentUser.getUsername().equals(this.list.get(position).getUsername())){
				ivPoint.setImageResource(R.drawable.green_point);
			}else{
				ivPoint.setImageResource(R.drawable.gray_point);
			}
			
			
			ivTrianle.setVisibility(View.VISIBLE);
			Button btnDelete = (Button) convertView.findViewById(R.id.btnAccountDelete);
//		    final int location = position;
		//	if(btnDelete.getVisibility()  == 0x00000000){
		    	btnDelete.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
					   WeiboUtil.myToastShow(context, "删除");
					   try {
						   HashMap params = new HashMap();
						   params.put("username", list.get(position).getUsername());
						   Task delAccountTask = new Task(Task.TASK_DEL_ACCOUNT,  params);
						   MainService.newTaskToTaskQueneHead(delAccountTask);
//						  new WeiboManager().delUser(context, list.get(position).getUsername());
//						  AccountManagerAdapter.this.notifyDataSetChanged();
					     
					   } catch (Exception e) {
						   WeiboUtil.myToastShow(context, "删除账号失败 ");
						  e.printStackTrace();
					   }
					  
					}
				});
		  //  }
		    list.get(position).getUsername();
			return convertView;	
		}
		
	}
	
 

}
